import tkinter as tk
from CustomWidget import DataNameModel,PlotGraphModel
from lib.data_procecss import Data_Process
from lib.log_info import LogError , LogInfo , LogWarning
import pandas as pd
import os
import datetime

class Page_Debug(tk.Frame):
    def __init__(self, master, data_process:Data_Process, **kwargs) -> None:
        super().__init__(master, **kwargs)
        self.id = 2
        self.data_process = data_process
        self.is_ploting = False
        self.data_name_moodel = DataNameModel(self)
        self.plot_graph_model = PlotGraphModel(self)
        self.after(10, self.UpdateDataNameList) # 10ms后更新数据名称列表
        self.after(10, self.UpdatePlot) # 10ms后更新数据名称列表
    
    def CreatePage(self,width:int,height:int) -> None:
        # 左边为数据名称列表，右边为绘图区域
        left_width = width*0.25
        right_width = width - left_width
        height = height - 20
        
        # 开始绘图按钮
        self.label_toggle_plot = tk.Label(self, bg='red')
        self.label_toggle_plot.place(x=10, y=10, width=30, height=30)
        
        self.button_toggle_Port = tk.Button(
                        self,
                        text='开始绘图',
                        font=('黑体', 12),
                        width=10,height=1,
                        command=self.TogglePlot
                    )
        self.button_toggle_Port.place(x=40, y=10, width=100, height=30)

        # 保存数据按钮
        self.button_save_data = tk.Button(
                        self,
                        text='保存数据',
                        font=('黑体', 12),
                        width=10,height=1,
                        command=self.SaveData
                    )
        self.button_save_data.place(x=40, y=40, width=100, height=30)

        # 数据名称列表模块
        self.data_name_moodel.place(x=0, y=90, width=left_width, height=height)
        # 图像绘制模块
        self.plot_graph_model.place(x=left_width, y=10, width=right_width, height=height)
        return

    ############################################################
    #  实时任务
    #  UpdateDataNameList 更新数据名称列表
    #  UpdatePlot 更新绘图
    ############################################################
    
    def UpdateDataNameList(self) -> None:
        '''更新数据名称列表'''
        data_name_list = list(self.data_process.debug_data.latest['datas'].keys())
        # print(data_name_list)
        self.data_name_moodel.UpdateDataNameList(data_name_list)
        self.data_name_moodel.UpdateCheckbuttons()
        
        self.after(100, self.UpdateDataNameList) # 100ms后更新数据名称列表
        return
    
    def UpdatePlot(self) -> None:
        if self.is_ploting:
            y = []
            y_labels = []
            
            for name in self.data_name_moodel.selected_names:
                y_labels.append(name)
                y.append(self.data_process.debug_data.storage['datas'][name][-500:])
            
            if len(self.data_name_moodel.selected_names) == 0:
                y = [[]]
                x = []
            else:
                x = self.data_process.debug_data.storage['time_stamp'][-500:]
                
            self.plot_graph_model.UpdateGraph(x,y,y_labels)
        
        self.after(50, self.UpdatePlot) # 50ms后更新图片
        return
    ############################################################
    #  回调函数
    #  TogglePlot 变更绘图状态
    #  SaveData 保存数据
    ############################################################
    
    def TogglePlot(self) -> None:
        if self.label_toggle_plot['bg'] == 'red':
            self.label_toggle_plot['bg'] = 'green'
            self.button_toggle_Port['text'] = '停止绘图'
            self.is_ploting = True
        else:
            self.label_toggle_plot['bg'] = 'red'
            self.button_toggle_Port['text'] = '开始绘图'
            self.is_ploting = False
        return
    
    def SaveData(self) -> None:
        # 保存调试的数据
        try:
            # 获取调试数据
            debug_data = {"time_stamp":self.data_process.debug_data.storage["time_stamp"]}
            for key in self.data_process.debug_data.storage["datas"]:
                debug_data[key] = self.data_process.debug_data.storage["datas"][key]
            # 将调试数据转换为DataFrame
            df = pd.DataFrame(debug_data)
            # 获取当前用户的桌面路径
            desktop_path = os.path.join(os.path.expanduser("~"), "Desktop")
            # 获取当前日期和时间
            current_time = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
            filename = os.path.join(desktop_path, f"debug_data_{current_time}.xlsx")
            # 保存DataFrame到Excel文件
            df.to_excel(filename, index=False)
            LogInfo("已保存数据")
        except:
            LogError("保存数据失败")
        return